//
//  BDMoneyRedEnvelopeTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/7/13.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMoneyRedEnvelopeTableViewCell.h"
#import "BDRedEnvelopeModel.h"
#import "BDUserModel.h"

@interface BDMoneyRedEnvelopeTableViewCell ()

@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIView *dashLine;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UILabel *limit_moneyLabel;
@property (nonatomic, strong) UILabel *tehuiLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *zoneLabel;

@property (nonatomic, strong) BDRedEnvelopeModel *redEnvelope;

@end

@implementation BDMoneyRedEnvelopeTableViewCell

- (void)configeWithModel:(id)model{
    BDRedEnvelopeModel *redEnvelope = model;
    self.redEnvelope = redEnvelope;
    
    self.limit_moneyLabel.text = [NSString stringWithFormat:ASLocalizedString(@"满%@元可用"), [NSDecimalNumber decimalNumberWithString:redEnvelope.limit_money]];
    self.tehuiLabel.text = ASLocalizedString(@"商家特惠");
    self.timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@到期"), redEnvelope.endTimeString];
    BDUserModel *user = [BDUserModel getCurrentUser];
    self.phoneLabel.text = [NSString stringWithFormat:ASLocalizedString(@"限账号手机号为%@"), user.mobile];
    
    if (redEnvelope.isEffective == YES) {
        self.zoneLabel.text = redEnvelope.conditions;
        self.zoneLabel.textColor = [UIColor colorWithHexString:@"#767676"];
        self.moneyLabel.textColor = [UIColor colorWithHexString:@"#ff0025"];
        self.limit_moneyLabel.textColor = [UIColor colorWithHexString:@"#767676"];
        self.tehuiLabel.textColor = [UIColor colorWithHexString:@"#111111"];
        self.timeLabel.textColor = [UIColor colorWithHexString:@"#111111"];
        self.phoneLabel.textColor = [UIColor colorWithHexString:@"#111111"];
    }else{
        self.zoneLabel.text = redEnvelope.reason;
        self.zoneLabel.textColor = [UIColor colorWithHexString:@"#ff0025"];
        self.moneyLabel.textColor = [UIColor colorWithHexString:@"#cccccc"];
        self.limit_moneyLabel.textColor = [UIColor colorWithHexString:@"#cccccc"];
        self.tehuiLabel.textColor = [UIColor colorWithHexString:@"#cccccc"];
        self.timeLabel.textColor = [UIColor colorWithHexString:@"#cccccc"];
        self.phoneLabel.textColor = [UIColor colorWithHexString:@"#cccccc"];
        
    }
    
    
    
    self.bkView.size = CGSizeMake(self.width - 30, self.height);
    self.bkView.centerX = self.width / 2.0;
    
    self.shapeLayer.frame = self.bkView.bounds;
    
    CGFloat radius = 5;
    
    UIBezierPath *bezierpath = [[UIBezierPath alloc] init];
    [bezierpath moveToPoint:CGPointMake(0, radius)];
    [bezierpath addLineToPoint:CGPointMake(0, self.height - 36.5 - radius)];
    [bezierpath addArcWithCenter:CGPointMake(0, self.height - 36.5) radius:radius startAngle:-M_PI / 2.0 endAngle:M_PI / 2.0 clockwise:YES];
    [bezierpath addLineToPoint:CGPointMake(0, self.height - radius)];
    [bezierpath addArcWithCenter:CGPointMake(radius, self.height - radius) radius:radius startAngle:M_PI endAngle:M_PI / 2.0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(self.bkView.width - radius, self.height)];
    [bezierpath addArcWithCenter:CGPointMake(self.bkView.width - radius, self.height - radius) radius:radius startAngle:M_PI / 2.0 endAngle:0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(self.bkView.width, self.height - 36.5)];
    [bezierpath addArcWithCenter:CGPointMake(self.bkView.width, self.height - 36.5) radius:radius startAngle:M_PI / 2.0 endAngle:M_PI / 2.0 * 3 clockwise:YES];
    [bezierpath addLineToPoint:CGPointMake(self.bkView.width, radius)];
    [bezierpath addArcWithCenter:CGPointMake(self.bkView.width - radius, radius) radius:radius startAngle:0 endAngle:-M_PI / 2.0 clockwise:NO];
    [bezierpath addLineToPoint:CGPointMake(radius, 0)];
    [bezierpath addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:-M_PI / 2.0 endAngle:-M_PI clockwise:NO];
    
    self.shapeLayer.path = bezierpath.CGPath;
    self.shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    self.shapeLayer.shadowColor = [UIColor colorWithHexString:@"#cccccc"].CGColor;
    self.shapeLayer.shadowOffset = CGSizeMake(0, 0);
    self.shapeLayer.shadowOpacity = 1;
    self.shapeLayer.shadowRadius = 1;
    
    self.dashLine.size = CGSizeMake(self.bkView.width - radius * 2, 1);
    [self.dashLine drawLineLength:5 lineSpacing:2 lineColor:[UIColor colorWithHexString:@"#cccccc"] verticality:NO];
    self.dashLine.centerY = self.height - 36.5;
    self.dashLine.left = self.bkView.left + radius;
    
    
    self.moneyLabel.font = [UIFont systemFontOfSize:13];
    NSString *moneyString = [NSString stringWithFormat:@"¥%@", [NSDecimalNumber decimalNumberWithString:self.redEnvelope.money]];
    NSMutableAttributedString *attribuString = [[NSMutableAttributedString alloc] initWithString:moneyString];
    NSRange range = [moneyString rangeOfString:[NSString stringWithFormat:@"¥%@", [NSDecimalNumber decimalNumberWithString:self.redEnvelope.money]]];
    [attribuString setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25]} range:range];
    self.moneyLabel.attributedText = attribuString;
    [self.moneyLabel sizeToFit];
    self.moneyLabel.top = 26;
    self.moneyLabel.centerX = self.bkView.left + 56;
    
    self.limit_moneyLabel.font = [UIFont systemFontOfSize:11];
    
    [self.limit_moneyLabel sizeToFit];
    self.limit_moneyLabel.top = self.moneyLabel.bottom + 10;
    self.limit_moneyLabel.centerX = self.moneyLabel.centerX;
    
    self.tehuiLabel.font = [UIFont systemFontOfSize:15];
    
    [self.tehuiLabel sizeToFit];
    self.tehuiLabel.top = 22;
    self.tehuiLabel.left = self.moneyLabel.centerX + 28 + 30;
    
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    
    [self.timeLabel sizeToFit];
    self.timeLabel.top = self.tehuiLabel.bottom + 14;
    self.timeLabel.left = self.tehuiLabel.left;
    
    self.phoneLabel.font = [UIFont systemFontOfSize:12];
    
    [self.phoneLabel sizeToFit];
    self.phoneLabel.top = self.timeLabel.bottom + 4;
    self.phoneLabel.left = self.tehuiLabel.left;
    
    self.zoneLabel.font = [UIFont systemFontOfSize:10];
    [self.zoneLabel sizeToFit];
    self.zoneLabel.centerY = self.height - 36.5 / 2.0;
    self.zoneLabel.left = self.bkView.left + 20;
    
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

- (CAShapeLayer *)shapeLayer{
    if (!_shapeLayer) {
        self.shapeLayer = [[CAShapeLayer alloc] init];
        [self.bkView.layer addSublayer:_shapeLayer];
    }
    return _shapeLayer;
}

- (UIView *)bkView{
    if (!_bkView) {
        self.bkView = [[UIView alloc] init];
        [self.contentView insertSubview:_bkView atIndex:0];
    }
    return _bkView;
}

- (UIView *)dashLine{
    if (!_dashLine) {
        self.dashLine = [[UIView alloc] init];
        [self.contentView addSubview:_dashLine];
    }
    return _dashLine;
}

- (UILabel *)moneyLabel{
    if (!_moneyLabel) {
        self.moneyLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_moneyLabel];
    }
    return _moneyLabel;
}

- (UILabel *)limit_moneyLabel{
    if (!_limit_moneyLabel) {
        self.limit_moneyLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_limit_moneyLabel];
    }
    return _limit_moneyLabel;
}

- (UILabel *)tehuiLabel{
    if (!_tehuiLabel) {
        self.tehuiLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_tehuiLabel];
    }
    return _tehuiLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        self.timeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (UILabel *)phoneLabel{
    if (!_phoneLabel) {
        self.phoneLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_phoneLabel];
    }
    return _phoneLabel;
}

- (UILabel *)zoneLabel{
    if (!_zoneLabel) {
        self.zoneLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_zoneLabel];
    }
    return _zoneLabel;
}
@end
